#!/usr/bin/env bash
#
# vim:et:ft=sh:sts=2:sw=2
#
#/**
# * Licensed to the Apache Software Foundation (ASF) under one
# * or more contributor license agreements.  See the NOTICE file
# * distributed with this work for additional information
# * regarding copyright ownership.  The ASF licenses this file
# * to you under the Apache License, Version 2.0 (the
# * "License"); you may not use this file except in compliance
# * with the License.  You may obtain a copy of the License at
# *
# *     http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# */

ARGV0=`basename "$0"`
LSB_RELEASE="/etc/lsb-release"
VERSIONS_SHELLS="/bin/bash /bin/sh"

true; TRUE=$?
false; FALSE=$?
ERROR=2

UNAME_R=`uname -r`
UNAME_S=`uname -s`

__versions_haveStrings=${ERROR}

versions_osName() {
  os_name_='unrecognized'
  os_system_=${UNAME_S}
  os_release_=${UNAME_R}
  case ${os_system_} in
    CYGWIN_NT-*) os_name_='Cygwin' ;;
    Darwin)
      os_name_=`/usr/bin/sw_vers -productName`
      os_version_=`versions_osVersion`
      case ${os_version_} in
        10.4|10.4.[0-9]*) os_name_='Mac OS X Tiger' ;;
        10.5|10.5.[0-9]*) os_name_='Mac OS X Leopard' ;;
        10.6|10.6.[0-9]*) os_name_='Mac OS X Snow Leopard' ;;
        10.7|10.7.[0-9]*) os_name_='Mac OS X Lion' ;;
        10.8|10.8.[0-9]*) os_name_='Mac OS X Mountain Lion' ;;
        10.9|10.9.[0-9]*) os_name_='Mac OS X Mavericks' ;;
        10.10|10.10.[0-9]*) os_name_='Mac OS X Yosemite' ;;
        10.11|10.11.[0-9]*) os_name_='Mac OS X El Capitan' ;;
        10.12|10.12.[0-9]*) os_name_='macOS Sierra' ;;
        10.13|10.13.[0-9]*) os_name_='macOS High Sierra' ;;
        *) os_name_='macOS' ;;
      esac
      ;;
    FreeBSD) os_name_='FreeBSD' ;;
    Linux) os_name_='Linux' ;;
    SunOS)
      os_name_='SunOS'
      if [ -r '/etc/release' ]; then
        if grep 'OpenSolaris' /etc/release >/dev/null; then
          os_name_='OpenSolaris'
        else
          os_name_='Solaris'
        fi
      fi
      ;;
  esac

  echo ${os_name_}
  unset os_name_ os_system_ os_release_ os_version_
}

versions_osVersion() {
  os_version_='unrecognized'
  os_system_=${UNAME_S}
  os_release_=${UNAME_R}
  case ${os_system_} in
    CYGWIN_NT-*)
      os_version_=`expr "${os_release_}" : '\([0-9]*\.[0-9]\.[0-9]*\).*'`
      ;;
    Darwin)
      os_version_=`/usr/bin/sw_vers -productVersion`
      ;;
    FreeBSD)
      os_version_=`expr "${os_release_}" : '\([0-9]*\.[0-9]*\)-.*'`
      ;;
    Linux)
      if [ -r '/etc/os-release' ]; then
          os_version_=`awk -F= '$1~/PRETTY_NAME/{print $2}' /etc/os-release \
            |sed 's/"//g'`
      elif [ -r '/etc/redhat-release' ]; then
        os_version_=`cat /etc/redhat-release`
      elif [ -r '/etc/SuSE-release' ]; then
        os_version_=`head -n 1 /etc/SuSE-release`
      elif [ -r "${LSB_RELEASE}" ]; then
        if grep -q 'DISTRIB_ID=Ubuntu' "${LSB_RELEASE}"; then
          # shellcheck disable=SC2002
          os_version_=`cat "${LSB_RELEASE}" \
            |awk -F= '$1~/DISTRIB_DESCRIPTION/{print $2}' \
            |sed 's/"//g;s/ /-/g'`
        fi
      fi
      ;;
    SunOS)
      if [ -r '/etc/release' ]; then
        if grep 'OpenSolaris' /etc/release >/dev/null; then  # OpenSolaris
          os_version_=`grep 'OpenSolaris' /etc/release |awk '{print $2"("$3")"}'`
        else  # Solaris
          major_=`echo "${os_release_}" |sed 's/[0-9]*\.\([0-9]*\)/\1/'`
          minor_=`grep Solaris /etc/release |sed 's/[^u]*\(u[0-9]*\).*/\1/'`
          os_version_="${major_}${minor_}"
        fi
      fi
      ;;
  esac

  echo "${os_version_}"
  unset os_release_ os_system_ os_version_ major_ minor_
}

versions_shellVersion() {
  shell_=$1

  shell_present_=${FALSE}
  case "${shell_}" in
    ash) [ -x '/bin/busybox' ] && shell_present_=${TRUE} ;;
    *) [ -x "${shell_}" ] && shell_present_=${TRUE} ;;
  esac
  if [ ${shell_present_} -eq ${FALSE} ]; then
    echo 'not installed'
    return ${FALSE}
  fi

  version_=''
  case ${shell_} in
    */sh)
      # This could be one of any number of shells. Try until one fits.
      version_=''
      [ -z "${version_}" ] && version_=`versions_shell_bash "${shell_}"`
      ;;
    */bash) version_=`versions_shell_bash "${shell_}"` ;;
    *) version_='invalid'
  esac

  echo "${version_:-unknown}"
  unset shell_ version_
}

versions_shell_bash() {
  $1 --version : 2>&1 |grep 'GNU bash' |sed 's/.*version \([^ ]*\).*/\1/'
}

versions_main() {
  # Treat unset variables as an error.
  set -u

  os_name=`versions_osName`
  os_version=`versions_osVersion`
  echo "os: ${os_name} version: ${os_version}"

  for shell in ${VERSIONS_SHELLS}; do
    shell_version=`versions_shellVersion "${shell}"`
    echo "shell: ${shell} version: ${shell_version}"
  done
}

if [ "${ARGV0}" = 'versions' ]; then
  versions_main "$@"
fi
